<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/7
 * Time: 1:51
 */

namespace app\api\controller\v3;

use app\api\controller\v2\Auth;
use app\common\lib\exception\ApiException;
use app\common\model\Recode;
use think\response\Json;

/**
 * @title 每日运势相关接口
 * @desc
 * @url
 * @version 1.3.0
 * Class Fortune
 * @package app\api\controller\v2
 */
class Fortune extends Auth
{
    protected $nodel;

    public function _initialize()
    {
        parent::_initialize();

        $this->nodel = config('code.nodel');
    }

    /**
     * @title 每日运势详情
     * @method GET
     * @desc
     * @return Json
     * @throws \app\common\lib\exception\ApiException
     */

    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }
        $rid = input('get.rid/d', 0);
        // 当前时间
        $time = strtotime(date('Y-m-d', input('get.time/d')));
        $uid = $this->user['id'];

        if (!$rid) throw new ApiException('档案信息获取失败');

        $recode = (new Recode())->getRecode(['id' => $rid]);

        if (!$recode || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }

//        $arr = (new \app\common\model\Fortune())->getUserUnlockFortune(['uid'=>$uid,'time'=>$time,'rid'=>$rid]);

        $arr = \app\common\cachemodel\FortuneCache::CheckFortuneSevenUnlockStatus($uid, $rid, $time);

        $is_unlock = $arr['is_unlock'];
        $seven = $arr['time'];//日记标识
//        $seven = $arr['seven'];//日记标识
        $fortune = $arr['fortune'];
        $selfId = $arr['selfId'];
        $note_status = $arr['note_status'];

        if ($is_unlock === true)
        {

            $is_use_cache = 1;
            if ($is_use_cache)
            {
                $map = ['time' => $time, 'rid' => $rid, 'uid' => (int)$this->user['id']];
                //$data = (new FortuneCache())->getDetailFortune($map);
                $data = (new \app\common\cachemodel\FortuneCache())->getDayFortune($map);
                $data['time'] = $seven;
                $data['fortune'] = $fortune;
            } else
            {
                $dailyFortune = new \app\everyday\controller\Index();

                $data = $dailyFortune->init($rid, strtotime(date('Y-m-d', $time) . ' 12:00:00'));
                $data['time'] = $seven;
                $data['fortune'][] = $fortune;
                $temp[] = $data['fortune_list'][4];
                $temp[] = $data['fortune_list'][3];
                $temp[] = $data['fortune_list'][1];
                $temp[] = $data['fortune_list'][0];
                $temp[] = $data['fortune_list'][6];
                $temp[] = $data['fortune_list'][2];
                $temp[] = $data['fortune_list'][5];
                $temp[] = $data['fortune_list'][7];
                $temp[] = $data['fortune_list'][8];
                //婚恋/学习/事业/财富/出行/健康/交际/家人/心情
                $data['fortune_list'] = $temp;
            }

        }

        // 去重
        $seven = array_unique($seven);
        // 重新索引
        $data['time'] = array_values($seven);

        // 去重
        $fortune = array_unique($fortune);
        // 重新索引
        $data['fortune'] = array_values($fortune);
        $data['self_rid'] = $selfId['id'];
        $data['note_status'] = $note_status;

        return show(200, '加载成功', $data);
    }
}
